pub fn parse_number_and_base(num_str: &str) -> Option<(String, String)> {
    if let Some(left_paren_index) = num_str.find('(') {
        if let Some(right_paren_index) = num_str.find(')') {
            let number = num_str[..left_paren_index].to_string();
            let base = num_str[left_paren_index + 1..right_paren_index].to_string();
            let convert_base = base.parse::<u32>().unwrap(); 
            
            if convert_base < 2 || convert_base > 16 {
                println!("Base not in range(2, 16).");
                return None;
            }
            return Some((number, base));
        }
    }
    None
}

pub fn convert_base(num_str: &str, to_base: u32) -> String {
    let (number, base) = parse_number_and_base(num_str).unwrap();
    // println!("number is {}, base is {}, to_base is {}", number, base, to_base);

    let base_from = base.parse::<u32>().unwrap();
    let num = u32::from_str_radix(&number, base_from);

    let mut result = String::new();
    let mut n = num.unwrap();
    while n > 0 {
        let remainder = n % to_base;
        if to_base == 16 {
            result.push_str(&format!("{:x}", remainder));
        }
        else {
            result.push_str(&format!("{:}", remainder));
        }
        n = n / to_base;
    }
    result.chars().rev().collect::<String>()
}

